package com.ecplatform.cart.controller;

import com.ecplatform.cart.pojo.vo.CartItemVO;
import com.ecplatform.cart.service.ICartService;
import com.ecplatform.common.exception.ItemException;
import com.ecplatform.common.pojo.po.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/cart")
@Slf4j
public class    CartController {

    private final ICartService cartService;

    @PutMapping("/add")
    public Result<List<CartItemVO>> addItemToCart(@RequestParam(value = "productId") Long productId,
                                                  @RequestParam(value = "quantity", defaultValue = "1") Integer quantity)
            throws ItemException {
        log.info("用户添加商品到购物车:商品ID = {}", productId);

        List<CartItemVO> cart = cartService.addItemToCart(productId, quantity);

        return Result.success(cart);
    }

    @GetMapping("/test")
    public String test() {
        return "test";
    }
}